package com.shenyong.ladmin.controller;

import com.shenyong.entity.LoginUsers;
import com.shenyong.exception.MyException;
import com.shenyong.ladmin.entity.Article;
import com.shenyong.ladmin.entity.Collection;
import com.shenyong.ladmin.entity.Note;
import com.shenyong.ladmin.entity.Users;
import com.shenyong.ladmin.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/UserLogin")
public class UserLogin {
    @Autowired
    private UsersService usersService;
    @RequestMapping("usersLogin")
    public Map<String, Object> usersLogin(Users users, HttpServletResponse httpServletResponse){
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        log.warn("执行前台登录操作!");
        System.out.println(users);
        LoginUsers use=usersService.usersLogin(users);
        try {
            if(use!=null){
                httpServletResponse.setHeader("Authorization",use.getToken());
                httpServletResponse.setHeader("Access-Control-Expose-Headers", "Authorization");
            }
            resultMap.put("loginUser",use);
            resultMap.put("user",use.getUser());
            System.out.println(resultMap);
        }catch (MyException e) {
            resultMap.put("msg",e.getMessage());
        }catch (LockedAccountException e) {
            resultMap.put("msg",e.getMessage());
        }catch (IncorrectCredentialsException e) {
            resultMap.put("msg",e.getMessage());
        }catch (AccountException e) {
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
    @RequestMapping("/logout")
    public boolean logout(HttpServletResponse  response){
        Subject lvSubject= SecurityUtils.getSubject();
        lvSubject.logout();
        response.setStatus(302);
        return true;

    }
    @RequestMapping("userAdd")
    public int userAdd(Users users){
        users.setUser_registerData(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        users.setUser_enabled("0");
        users.setUser_levelId(1);
        users.setUser_score(0);
        return usersService.userAdd(users);
    }
    @RequestMapping("saveUsers")
    public LoginUsers saveUsers(Users users){
        if(usersService.updateUser(users)==1) {
            return usersService.usersLogin(users);
        }
        return null;
    }
    @RequestMapping("savePassword")
    public int savePassword(String passworda,String passwordb,String user_id){
        Map<String,Object> map=new HashMap<>();
        map.put("passworda",passworda);
        map.put("passwordb",passwordb);
        map.put("user_id",user_id);
        return usersService.savePassword(map);
    }
    @RequestMapping("getFriends")
    public List<Users> getFriends(Users users) {
        return usersService.getFriends(users);
    }
    @RequestMapping("randNote")
    public Note randNote(){
        return usersService.randNote();
    }

    @RequestMapping("getCollections")
    public List<Article> getCollections(Collection collection){
        return usersService.getCollections(collection);
    }
}
